package control.panel
{
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	
	import global.GameState;
	
	import model.hall.ListData;
	
	import utils.DisplayTool;
	import utils.EnterFrameTimer;
	
	import view.hall.ListItem;
	import view.panel.PlayerDiscribePanel;

	public class PlayerItemPanelControl extends PanelControl
	{
		private var selfCloseTimer:EnterFrameTimer;
		private var data:ListData;
		
		public function PlayerItemPanelControl()
		{
			initPanel();
			initTimer();
		}
		
		private function initTimer():void
		{
			selfCloseTimer = new EnterFrameTimer(2000);
			selfCloseTimer.addEventListener(TimerEvent.TIMER,onTimer);
		}
		
		private function onTimer(event:TimerEvent):void
		{
			_view.playerItemPanel.close();
			selfCloseTimer.reset();
		}
		
		private function initPanel():void
		{
			_view.playerItemPanel = new PlayerDiscribePanel(this);
		}
		
		public function open(target:ListItem):void
		{
			
			
			
			selfCloseTimer.reset();
			_view.playerItemPanel.open();
			var point:Point = DisplayTool.getGlobalXY(target);
			
			if (point.y + _view.playerItemPanel.height > GameState.GAME_HEIGHT)
			{
				point.y = GameState.GAME_HEIGHT - _view.playerItemPanel.height;
			}
			
			_view.playerItemPanel.x = point.x - _view.playerItemPanel.width;
			_view.playerItemPanel.y = point.y;
			addStageListener();
			addPanelListener();
			
			data = target.data;
			
			_view.playerItemPanel.update(data);
		}
		
		public function onTrace():void
		{
			if (data)
			{
				var serverIp:String = data.data.@serverIp.toString();
				var port:int = int(data.data.@port);
				var roomId:String = data.data.@roomId.toString();
				
				if (serverIp == "" || roomId == "")
				{
					return;
				}
				GameState.isQuickGame = 0;
				_model.loginModel.loginGameServer(roomId,serverIp,port);
			}
			//var serverIp:String = data.	
		}
		
		private function addPanelListener():void
		{
			_view.playerItemPanel.addEventListener(MouseEvent.ROLL_OVER,onPanelROver);
			_view.playerItemPanel.addEventListener(MouseEvent.ROLL_OUT,onPanelROut);
		}
		
		private function onPanelROut(event:MouseEvent):void
		{
			selfCloseTimer.start();
		}
		
		private function onPanelROver(event:MouseEvent):void
		{
			selfCloseTimer.reset();
		}
		
		private function addStageListener():void
		{
			_view.stage.addEventListener(MouseEvent.MOUSE_MOVE,onStageMove)
		}
		
		private function onStageMove(event:MouseEvent):void
		{
			_view.stage.removeEventListener(MouseEvent.MOUSE_MOVE,onStageMove);
			selfCloseTimer.start();
		}
	}
}